package com.caiyi.financial.nirvana.sms.filters;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.entity.SmsUser;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.enums.SmsUserTypeEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.param.SendMsgParam;
import com.caiyi.financial.nirvana.sms.util.CommUtil;
import com.youyu.common.utils.YyAssert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public class ParamsFilter implements Filter {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void run(FilterChain filterChain) {
        SendMsgParam param = filterChain.getParam();
        logger.info("SendParamsFilter : {}", param);
        String account = param.getAccount();
        String phones = param.getPhones();
        String content = param.getContent();
        String uid = param.getUid();
        String notifyUrl = param.getNotifyUrl();
        /** 参数验证 **/
        YyAssert.isFalse(StringUtils.isBlank(account), ReturnCodeEnum.PARAMS_ERROR.code, "account不能为空");
        YyAssert.isFalse(account.length() > 50, ReturnCodeEnum.PARAMS_ERROR.code, "account长度不能超过50位");
        YyAssert.isFalse(StringUtils.isBlank(phones), ReturnCodeEnum.PARAMS_ERROR.code, "phones不能为空");
        YyAssert.isFalse(StringUtils.isBlank(content), ReturnCodeEnum.PARAMS_ERROR.code, "content不能为空");
        YyAssert.isFalse(content.length() > 536, ReturnCodeEnum.PARAMS_ERROR.code, "content内容不能超过536个字符");
        YyAssert.isFalse(StringUtils.isNotBlank(uid) && uid.length() > 50, ReturnCodeEnum.PARAMS_ERROR.code, "uid长度不能超过50个字符");
        YyAssert.isFalse(StringUtils.isNotBlank(notifyUrl) && notifyUrl.length() > 255, ReturnCodeEnum.PARAMS_ERROR.code, "notifyUrl长度不能超过255个字符");
        String key = String.format(JedisKey.SMS_USER, account);
        String userJson = JedisService.get(key);
        key = String.format(JedisKey.SMS_BALANCE, account);
        String smsNum = JedisService.get(key);
        YyAssert.isFalse(StringUtils.isBlank(userJson) || StringUtils.isBlank(smsNum), ReturnCodeEnum.ACCOUNT_ERROR);
        //设置用户信息
        SmsUser user = JSON.parseObject(userJson, SmsUser.class);
        user.setSmsNum(Integer.parseInt(smsNum));
        filterChain.setSmsUser(user);
        //替换号码之间的空格
        phones = phones.replace(" ", "");
        //设置手机号码
        param.setPhones(phones);
        //设置phoneList
        String[] phoneArr = phones.split(",");
        Set<String> phoneSet = new HashSet<>(phoneArr.length);
        for (String phone : phoneArr) {
            if (StringUtils.isBlank(phone)) {
                continue;
            }
            if (!StringUtils.isNumeric(phone) || phone.length() != 11) {
                continue;
            }
            int operator = CommUtil.getPhoneOperator(phone);
            if (operator == 0) {
                continue;
            }
            phoneSet.add(phone);
        }
        //行业账号最多一次提交10个号码
        YyAssert.isFalse(user.getType() == SmsUserTypeEnum.INDUSTRY.index && phoneSet.size() > 10, ReturnCodeEnum.ACCOUNT_ERROR.code, "行业账号phone最多发送10个");
        //营销账号最多一次提交1000个号码
        YyAssert.isFalse(phoneSet.size() > 1000, ReturnCodeEnum.ACCOUNT_ERROR.code, "营销账号phone最多发送1000个");
        YyAssert.isFalse(phoneSet.isEmpty(), ReturnCodeEnum.ACCOUNT_ERROR.code, "手机号码格式不正确");
        filterChain.setPhones(phoneSet);
        filterChain.doFilter();
    }

}
